package stage
{
	import edu.pku.component.SensorHistory;
	import edu.pku.controller.util.RegionPoint;
	import edu.pku.controller.util.SubjectDescription;
	import edu.pku.sandbox.SensorHistoryData;
	
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	
	import mx.collections.ArrayCollection;
	import mx.managers.PopUpManager;

	/**
	 * sandtable subject is represent a subject in the sandtable
	 * @author power
	 */
	public class SandTableSubject extends OrderSprite
	{
		public static var MAX_RECORDS:int=100;

		protected var Threshold_Value:int=500;

		public var id:String=null;

		public var historyRecords:ArrayCollection;

		public var opened:Boolean=false;

		public var type:int=AppConstant.TYPE_UNKNOWN;

		protected var isWarning:Boolean=false;
		
		/**
		 * 
		 * @param historyData
		 * @return 
		 */
		public function shouldWarn(historyData:SensorHistoryData):Boolean
		{
			//pressure is not the same
			if(this.type==AppConstant.TYPE_PRESSURE_SENSOR)
			{
				return (historyData.value as int) > Threshold_Value;
			}
			//now if the senaor have warn the value is 1, other wise it is zero
			return (historyData.value as int) > 0;
		}
		
		public function clearHistoryRecords():void
		{
			if(historyRecords!=null)
				historyRecords.removeAll();
		}
		
		private  static  var Init_Alaph:Number=1;
		
		public function SandTableSubject(id:String, type:int)
		{
			super(0);
			this.alpha=Init_Alaph;
			this.id=id;
			if(id!="TANK")
				this.addEventListener(MouseEvent.CLICK, openInfoWindow);
			historyRecords=new ArrayCollection();
			this.type=type;
		}
		
		
		/**
		 * Nimbus Effect
		 */
		public function EffectNimbus():void
		{
			var child:NimbusSprite=getChildAt(0) as NimbusSprite;
			child.effectNimbus();	
		}

		//create different sensors
		public static function createSubjectByDescription(subdes:SubjectDescription):SandTableSubject
		{
			//infrared subject is different
			if(subdes.type==AppConstant.TYPE_INFRARED_SENSOR)
			{
				var infrasprite:InfraRedSensorSubject=new InfraRedSensorSubject(subdes.id);
				infrasprite.addSecondPosition(subdes.x1-subdes.x,subdes.y1-subdes.y);
				infrasprite.x=subdes.x-infrasprite.getChildAt(0).width/2;
				infrasprite.y=subdes.y-infrasprite.getChildAt(0).width/2;
				infrasprite.displayOrder=AppConstant.ORDER_MIDDLE;
				return infrasprite;
			}
			else if(subdes.type==AppConstant.TYPE_RFID_READER)
			{
				var rfidsensor:RfidReaderSubject=new RfidReaderSubject(subdes.id);
				rfidsensor.x=subdes.x-rfidsensor.width/2;
				rfidsensor.y=subdes.y-rfidsensor.height/2;
				return rfidsensor;
			}
			var sprite:SandTableSubject;
			if(subdes.type==AppConstant.TYPE_TANK)
			{
				sprite=new SandTableSubject(subdes.id, subdes.type);
				var ImageProtypeClass:Class=ResourceManager.getImageClass(subdes.type);
				var imageInstance:DisplayObject=new ImageProtypeClass() as DisplayObject;
				sprite.addChild(imageInstance);
				//set the display order
				sprite.displayOrder=AppConstant.ORDER_TOP;
			}
			else
			{
				sprite=new SandTableSubject(subdes.id, subdes.type);
				var nimbusInstance:NimbusSprite=NimbusSprite.createNimbusSprite(subdes.type);
				sprite.addChild(nimbusInstance);
				//set the display order
				sprite.displayOrder=AppConstant.ORDER_BOTTOM;	
			}
			//set sprite position
			sprite.x=subdes.x-sprite.width/2;
			sprite.y=subdes.y-sprite.height/2;
			
			//set the threshold of the Pressuser Sensor
			if(sprite.type==AppConstant.TYPE_PRESSURE_SENSOR)
			{
				sprite.Threshold_Value=subdes.threshold;
			}
			return sprite;
		}

		public function updateData(historyData:SensorHistoryData):void
		{
			if (historyRecords.length > MAX_RECORDS)
			{
				historyRecords.removeItemAt(0);
			}
			historyRecords.addItem(historyData);

			if (shouldWarn(historyData))
			{
				
				//blink();
				EffectNimbus();
			}
		}

		public function getSensorTypeName(type:int):String
		{
			if(type==AppConstant.TYPE_INFRARED_SENSOR)
			{
				return ResourceManager.LABEL_INFRARED;
			}
			else if(type==AppConstant.TYPE_PRESSURE_SENSOR)
			{
				return ResourceManager.LABEL_PRESSURE;
			}
			else if(type==AppConstant.TYPE_METAL_SENSOR)
			{
				return ResourceManager.LABEL_METAL;
			}
			else if(type==AppConstant.TYPE_RFID_READER)
			{
				return ResourceManager.LABEL_RFIDINFOBOX;
			}
			else if(type==AppConstant.TYPE_SOUND_SENSOR)
			{
				return ResourceManager.LABEL_SOUND;
			}
			else if(type==AppConstant.TYPE_VIBRATE_SENSOR)
			{
				return ResourceManager.LABEL_VIBRATE;
			}
			return "";

		}
		public function openInfoWindow(event:MouseEvent):void
		{
			if (opened == false)
			{
				opened=true;

				var point:Point=new Point();

				point.x=event.stageX;
				point.y=event.stageY;

				var history:SensorHistory=SensorHistory(PopUpManager.createPopUp(this, SensorHistory, false));

				history.x=point.x + 10;
				history.y=point.y + 10;

				history.sandTableSubject=this;

				history.title=getSensorTypeName(type)+":"+id;

				history.historyData=historyRecords;
			}

		}

		public function blink():void
		{
//==================new one========================
			var instance:SandTableSubject=this;
			if(!isWarning)
			{
				isWarning=true;
				var inteval:uint=setInterval(function():void
				{
					if(alpha<1.0)
					{
						alpha=(alpha+0.2)>1.0?1.0:alpha+0.2;
					}
					else
					{
						clearInterval(inteval);
						isWarning=false;
						alpha=Init_Alaph;
					}
				},100);
			}
//==================new one========================
//==================old one=========================
//			var instance:SandTableSubject=this;
//			if(!isWarning)
//			{
//				this.visible=false;
//				isWarning=true;
//				setTimeout(function():void
//				{
//					instance.visible=true;
//					instance.isWarning=false;
//				},300);
//			}
//==================old  one==========================
//=========================older one======================
//			if (!isWarning)
//			{
//				var inteval:uint=setInterval(function():void
//					{
//						instance.visible=!instance.visible;
//					}, 500);
//				setTimeout(function():void
//					{
//						clearInterval(inteval);
//						instance.visible=true;
//						instance.isWarning=false;
//					}, 1500);
//			}
//			isWarning=true;
//==========================older one======================
		}
		
		public function getDrawnLinePoint():RegionPoint
		{
			var p:RegionPoint=new RegionPoint();
			p.x=this.x+this.width/2;
			p.y=this.y+this.height/2;
			p.radius=0;
			return p;
		}
	}
}